﻿// (c) Copyright Microsoft Corporation.
// This source is subject to [###LICENSE_NAME###].
// Please see [###LICENSE_LINK###] for details.
// All other rights reserved.

using System;
using System.Globalization;

namespace EnsureWeb.Controls
{
    /// <summary>
    /// The year view of time view scale.
    /// </summary>
    public class YearView : TimeView
    {
        /// <summary>
        /// Initializes a new instances of the YearView class.
        /// </summary>
        /// <param name="timeline">Timeline of the view.</param>
        public YearView(MultiScaleTimeline timeline)
            : base(timeline, TimelineScale.Year)
        {
        }

        /// <summary>
        /// Gets the display start date.
        /// </summary>
        /// <param name="date">Input date value.</param>
        /// <returns>The display start date.</returns>
        public static DateTime GetDisplayDateStart(DateTime date, YearOfDecade firstYearOfDecade)
        {
            int year = date.Year;
            int firstYear;
            int yearsOfDecade = DecadeView.YearsOfDecade;
            int endNum = Convert.ToInt16(firstYearOfDecade,CultureInfo.InvariantCulture);

            if (year % yearsOfDecade == endNum)
            {
                firstYear = year;
            }
            else if (year % yearsOfDecade > endNum)
            {
                firstYear = year / yearsOfDecade * yearsOfDecade + endNum;
            }
            else
            {
                firstYear = year / yearsOfDecade * yearsOfDecade + endNum - yearsOfDecade;
            }

            return new DateTime(firstYear, 1, 1);
        }

        /// <summary>
        /// Gets the start of display date range.
        /// </summary>
        public override DateTime BeginDate
        {
            get
            {
                return YearView.GetDisplayDateStart(CurrentDate, this.FirstYearOfDecade);
            }
        }

        /// <summary>
        /// Gets the end of display date range.
        /// </summary>
        public override DateTime EndDate
        {
            get
            {
                int startYear = BeginDate.Year;
                int endYear = startYear + 9;
                return new DateTime(endYear, 12, 31);
            }
        }

        /// <summary>
        /// Gets the number of sub items.
        /// </summary>
        protected override int ItemsCount
        {
            get
            {
                return 10;
            }
        }

        /// <summary>
        /// Move forward the display date, used when animation out of display.
        /// </summary>
        public override void NextPage()
        {
            UpdateDate(this.BeginDate.AddYears(10));
        }

        /// <summary>
        /// Move backward the display date, used when animation out of display.
        /// </summary>
        public override void PreviousPage()
        {
            UpdateDate(this.BeginDate.AddYears(-10));
        }

        /// <summary>
        /// Gets the next selectable date value.
        /// </summary>
        public override DateTime NextSelectableDate
        {
            get
            {
                return new DateTime(this.SelectedDate.Year + 1, 1, 1);
            }
        }

        /// <summary>
        /// Gets the timeline item text by index.
        /// </summary>
        /// <param name="index">Item index.</param>
        /// <returns>The timeline item text.</returns>
        protected override string GetTimelineItemText(int index)
        {
            string year = (this.BeginDate.Year + index).ToString(CultureInfo.InvariantCulture);
            return year;
        }

        /// <summary>
        /// Gets a value indicating whether the date is selected.
        /// </summary>
        /// <param name="dateTime">The checked date.</param>
        /// <returns>True if the date is selected.</returns>
        public override bool IsDateSelected(DateTime dateTime)
        {
            bool isSel = dateTime.Year == SelectedDate.Year;
            return isSel;
        }

        /// <summary>
        /// Check whether the item is selectable.
        /// </summary>
        /// <param name="index">The index of the item.</param>
        /// <returns>If the item is selectable.</returns>
        protected override DateTime GetTimelineItemDate(int index)
        {
            DateTime itemDate = this.BeginDate.AddYears(index);
            return itemDate;
        }

        /// <summary>
        /// Check if a item is selectable.
        /// </summary>
        /// <param name="index">The item index.</param>
        /// <returns>true if the item is selectable.</returns>
        public override bool IsItemSelectable(int index)
        {
            DateTime dateStart = this.GetTimelineItemDate(index);
            DateTime dateEnd = new DateTime(dateStart.Year, 12, 31);

            bool canSel = IsDateRangeSelectable(dateStart, dateEnd);
            return canSel;
        }

        /// <summary>
        /// Gets the rollup the text.
        /// </summary>
        public override string RollupText
        {
            get
            {
                int yearStart = this.BeginDate.Year;
                int yearEnd = yearStart + 9;

                return string.Format(CultureInfo.InvariantCulture, "{0} - {1}", yearStart, yearEnd);
            }
        }

        /// <summary>
        /// Gets the rollup date.
        /// </summary>
        protected override DateTime RollupDate
        {
            get
            {
                return DecadeView.GetDisplayDateStart(this.CurrentDate, this.FirstYearOfDecade);
            }
        }
    }
}
